﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormApplication.FormBase;
using WindowsFormsView;
using WindowsFormsModel.BusinessObjects;
using WindowsFormsPresenter.RestaurantTable;
using Infragistics.Win.UltraWinEditors;
using WindowsFormsPresenter.RestaurantArea;

namespace WindowsFormApplication.FormCategory
{
    public partial class frmRestaurantTableList : frmBaseCategory, IRestaurantTablesView, IRestaurantAreasView
    {
        private RestaurantTablesPresenter _restaurantTablesPresenter;
        private RestaurantAreasPresenter _restaurantAreasPresenter;

        private static int totalPage = 0;
        private static int currentPage = 0;

        public IList<RestaurantTableModel> RestaurantTables
        {
            set
            {
                var restaurantTables = value;
                grdMain.DataSource = restaurantTables;
                for (int i = 0; i < grdMain.DisplayLayout.Bands[0].Columns.Count; i++) { grdMain.DisplayLayout.Bands[0].Columns[i].Hidden = true; }
                grdMain.DisplayLayout.Bands[0].Columns["RestaurantTableCode"].Hidden = false;
                grdMain.DisplayLayout.Bands[0].Columns["RestaurantTableName"].Hidden = false;
                grdMain.DisplayLayout.Bands[0].Columns["RestaurantAreaID"].Hidden = false;
                grdMain.DisplayLayout.Bands[0].Columns["MaxPeople"].Hidden = false;
                grdMain.DisplayLayout.Bands[0].Columns["Description"].Hidden = false;

                grdMain.DisplayLayout.Bands[0].Columns["RestaurantTableCode"].Header.Caption = "Mã bàn ăn";
                grdMain.DisplayLayout.Bands[0].Columns["RestaurantTableName"].Header.Caption = "Tên bàn ăn";
                grdMain.DisplayLayout.Bands[0].Columns["RestaurantAreaID"].Header.Caption = "Tên khu vực";
                grdMain.DisplayLayout.Bands[0].Columns["MaxPeople"].Header.Caption = "SL người tối đa";
                grdMain.DisplayLayout.Bands[0].Columns["Description"].Header.Caption = "Mô tả";

                grdMain.DisplayLayout.Bands[0].Columns["RestaurantTableCode"].Header.VisiblePosition = 0;
                grdMain.DisplayLayout.Bands[0].Columns["RestaurantTableName"].Header.VisiblePosition = 1;
                grdMain.DisplayLayout.Bands[0].Columns["RestaurantAreaID"].Header.VisiblePosition = 2;
                grdMain.DisplayLayout.Bands[0].Columns["MaxPeople"].Header.VisiblePosition = 3;
                grdMain.DisplayLayout.Bands[0].Columns["Description"].Header.VisiblePosition = 4;
            }
        }
        public IList<RestaurantAreaModel> RestaurantAreas 
        {
            set
            {
                foreach (var item in value)
                    cboRestaurantArea.Items.Add(item.RestaurantAreaID, item.RestaurantAreaName);

                if (grdMain.Rows.Count > 0)
                {
                    grdMain.DisplayLayout.Bands[0].Columns["RestaurantAreaID"].EditorComponent = cboRestaurantArea;
                    grdMain.DisplayLayout.Bands[0].Columns["RestaurantAreaID"].CellActivation = Infragistics.Win.UltraWinGrid.Activation.NoEdit;
                }
            }
        }

        protected override void InitData()
        {
            try
            {
                totalPage = _restaurantTablesPresenter.GetTotalPageByBranch();
                _restaurantTablesPresenter.DisplayByPageAndBranch(currentPage);
                _restaurantAreasPresenter.DisplayByBranch();
                toolbarsManager.Tools["lblPage"].SharedProps.Caption = "Số trang " + (currentPage + 1) + "/ " + (totalPage + 1);
            }
            catch (ApplicationException ex) { ShowMessageBox(ex.Message, "Thông báo lỗi từ hệ thống !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        protected override void NextPage()
        {
            if (currentPage < totalPage)
            {
                currentPage = currentPage + 1;
                InitData();
            }
        }

        protected override void PreviousPage()
        {
            if (currentPage > 0)
            {
                currentPage = currentPage - 1;
                InitData();
            }
        }

        protected override void DeleteRow()
        {
            try
            {
                if (grdMain.ActiveRow.Index >= 0)
                {
                    string stockId = grdMain.ActiveRow.Cells[TablePrimaryKey].Value.ToString();
                    new RestaurantTablePresenter(null).Delete(stockId);
                    currentPage = 0;
                    ShowMessageBox("Xóa dữ liệu hoàn thành !", "Thông báo xóa dữ liệu !", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    ShowMessageBox("Bạn chưa chọn dòng để xóa !", "Thông báo xóa dữ liệu !", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (ApplicationException ex) { ShowMessageBox(ex.Message, "Thông báo lỗi hệ thống !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        public frmRestaurantTableList()
        {
            InitializeComponent();

            _restaurantTablesPresenter = new RestaurantTablesPresenter(this);
            _restaurantAreasPresenter = new RestaurantAreasPresenter(this);
        }
    }
}